package io.github.wppli.domain.authenticate.service.engine.impl;

import io.github.wppli.domain.authenticate.repository.IAuthenticateRepository;
import io.github.wppli.domain.authenticate.service.engine.IMessageLoader;
import io.github.wppli.sms.domain.SendSmsUtil;
import io.github.wppli.types.common.Validator;
import org.springframework.stereotype.Service;


/**
 * @author li--jiaqiang 2024−12−23
 */
@Service("phoneMessageLoader")
public class PhoneMessageLoader implements IMessageLoader {

    private final SendSmsUtil sendSmsService;
    private final IAuthenticateRepository userRepository;
    public PhoneMessageLoader(SendSmsUtil sendSmsService, IAuthenticateRepository userRepository) {
        this.sendSmsService = sendSmsService;
        this.userRepository = userRepository;
    }



    /**
     * 发送验证码
     * @param code 验证码
     * @param contactInformation 手机号
     */
    @Override
    public void sendCode(String code, String contactInformation) {
        boolean validPhoneNumber = Validator.isValidPhoneNumber(contactInformation);
        if (!validPhoneNumber) {
            throw new IllegalArgumentException("手机号格式不正确");
        }
        sendSmsService.sendSMS(contactInformation, code);
    }

    @Override
    public void setPassword(String phoneNumber, String encodedPassword) {
        userRepository.setUserPasswordWithPhoneNumber(phoneNumber, encodedPassword);
    }


}